﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DefaultController.cs" company="CodePlex">
//   Copyright © CodePlex 2012
// </copyright>
// <summary>
//   Defines the ContactController type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Example.Business
{
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;

    using DataFramework;

    using Example.Common;
    using Example.Data;

    /// <summary>
    /// Contains the business logic around contacts.
    /// </summary>
    /// <remarks>
    /// NOTE: The TransactionManager examples here are a bit overkill in their current form
    /// but if your code needed to touch multiple tables, you could create multiple dao's and
    /// perform all your needed actions in one transaction. 
    /// </remarks>
    public class DefaultController
    {
        /// <summary>
        /// Fetches all contacts.
        /// </summary>
        /// <returns>A data table containing the contacts in the system.</returns>
        public DataTable FetchAllContacts()
        {
            var dao = new ContactDao();
            return dao.LoadAllAsTable();
        }

        /// <summary>
        /// Fetches all as objects.
        /// </summary>
        /// <returns>A list of all the contacts in the system.</returns>
        public List<Contact> FetchAllAsObjects()
        {
            var dao = new ContactDao();
            return dao.LoadAll().ToList();
        }

        /// <summary>
        /// Creates the new contact.
        /// </summary>
        /// <param name="newContact">The new contact.</param>
        public void CreateNewContact(Contact newContact)
        {
            try
            {
                var dao = new ContactDao();

                TransactionManager.StartTransaction();
                dao.Add(newContact);
                TransactionManager.CommitTransaction();
            }
            catch
            {
                TransactionManager.RollbackTransaction();
            }
        }

        /// <summary>
        /// Bulks the create new contact.
        /// </summary>
        /// <param name="newContact">The new contact.</param>
        public void BulkCreateNewContact(IEnumerable<Contact> newContact)
        {
            try
            {
                var dao = new ContactDao();

                TransactionManager.StartTransaction();

                foreach (var contact in newContact)
                {
                    dao.Add(contact);
                }

                TransactionManager.CommitTransaction();
            }
            catch
            {
                TransactionManager.RollbackTransaction();
            }
        }

        /// <summary>
        /// Updates the contact.
        /// </summary>
        /// <param name="contact">The contact.</param>
        public void UpdateContact(Contact contact)
        {
            try
            {
                var dao = new ContactDao();

                TransactionManager.StartTransaction();
                dao.Update(contact);
                TransactionManager.CommitTransaction();
            }
            catch
            {
                TransactionManager.RollbackTransaction();
            }
        }

        /// <summary>
        /// Removes the contact.
        /// </summary>
        /// <param name="contact">The contact.</param>
        public void RemoveContact(Contact contact)
        {
            try
            {
                var dao = new ContactDao();

                TransactionManager.StartTransaction();
                dao.Remove(contact);
                TransactionManager.CommitTransaction();
            }
            catch
            {
                TransactionManager.RollbackTransaction();
            }
        }

        /// <summary>
        /// Bulk Removes contacts.
        /// </summary>
        /// <param name="contacts">The contacts.</param>
        public void BulkRemoveContacts(IEnumerable<Contact> contacts)
        {
            try
            {
                var dao = new ContactDao();

                TransactionManager.StartTransaction();

                foreach (var contact in contacts)
                {
                    dao.Remove(contact);
                }

                TransactionManager.CommitTransaction();
            }
            catch
            {
                TransactionManager.RollbackTransaction();
            }
        }
    }
}
